package cn.schoolwow.quickflow.service.flowContext.putInstanceData;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.domain.FlowDataFeature;
import cn.schoolwow.quickflow.domain.FlowExecutorConfig;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.service.flowContext.putData.FlowContextPutDataOperation;
import cn.schoolwow.quickflow.util.QuickFlowUtil;

public class FlowContextPutInstanceDataOperationImpl implements FlowContextPutInstanceDataOperation {
    private FlowExecutorConfig flowExecutorConfig;
    private FlowContextPutDataOperation flowContextPutDataOperation;

    public FlowContextPutInstanceDataOperationImpl(FlowExecutorConfig flowExecutorConfig, FlowContextPutDataOperation flowContextPutDataOperation) {
        this.flowExecutorConfig = flowExecutorConfig;
        this.flowContextPutDataOperation = flowContextPutDataOperation;
    }

    @Override
    public FlowContext putThreadLocalInstanceData(Object instance) {
        return putInstanceData(instance, FlowContextDataRange.ThreadLocal);
    }

    @Override
    public FlowContext putThreadLocalInstanceData(Object instance, Class classType) {
        return putInstanceData(instance, classType, FlowContextDataRange.ThreadLocal);
    }

    @Override
    public FlowContext putCurrentFlowInstanceData(Object instance) {
        return putInstanceData(instance, FlowContextDataRange.CurrentFlow);
    }

    @Override
    public FlowContext putCurrentFlowInstanceData(Object instance, Class classType) {
        return putInstanceData(instance, classType, FlowContextDataRange.CurrentFlow);
    }

    @Override
    public FlowContext putCurrentCompositeFlowInstanceData(Object instance) {
        return putInstanceData(instance, FlowContextDataRange.CurrentCompositeBusiness);
    }

    @Override
    public FlowContext putCurrentCompositeFlowInstanceData(Object instance, Class classType) {
        return putInstanceData(instance, classType, FlowContextDataRange.CurrentCompositeBusiness);
    }

    @Override
    public FlowContext putTemporaryInstanceData(Object instance) {
        return putInstanceData(instance, FlowContextDataRange.Flow, FlowDataFeature.TemporaryData);
    }

    @Override
    public FlowContext putTemporaryInstanceData(Object instance, Class classType) {
        return putInstanceData(instance, classType, FlowContextDataRange.Flow, FlowDataFeature.TemporaryData);
    }

    @Override
    public FlowContext putInstanceData(Object instance, FlowDataFeature... flowDataFeatures) {
        return putInstanceData(instance, FlowContextDataRange.Flow, flowDataFeatures);
    }

    @Override
    public FlowContext putInstanceData(Object instance, Class classType, FlowDataFeature... flowDataFeatures) {
        if(null==instance){
            return flowExecutorConfig.flowContext;
        }
        String key = classType.getName();
        return flowContextPutDataOperation.putFlowContextData(key, instance, FlowContextDataRange.Flow, flowDataFeatures);
    }

    @Override
    public FlowContext putContextThreadLocalInstanceData(Object instance, FlowDataFeature... flowDataFeatures) {
        return putInstanceData(instance, FlowContextDataRange.ContextThreadLocal);
    }

    @Override
    public FlowContext putContextThreadLocalInstanceData(Object instance, Class classType, FlowDataFeature... flowDataFeatures) {
        return putInstanceData(instance, classType, FlowContextDataRange.ContextThreadLocal);
    }

    @Override
    public FlowContext putContextInstanceData(Object instance) {
        return putInstanceData(instance, FlowContextDataRange.Context);
    }

    @Override
    public FlowContext putContextInstanceData(Object instance, Class classType) {
        return putInstanceData(instance, classType, FlowContextDataRange.Context);
    }

    @Override
    public FlowContext putContextTemporaryInstanceData(Object instance) {
        return putInstanceData(instance, FlowContextDataRange.Context, FlowDataFeature.TemporaryData);
    }

    @Override
    public FlowContext putContextTemporaryInstanceData(Object instance, Class classType) {
        return putInstanceData(instance, classType, FlowContextDataRange.Context, FlowDataFeature.TemporaryData);
    }

    @Override
    public FlowContext putInstanceData(Object instance, FlowContextDataRange flowContextDataRange, FlowDataFeature... flowDataFeatures) {
        if(null==instance){
            return flowExecutorConfig.flowContext;
        }
        String key = QuickFlowUtil.getInstanceClassName(instance);
        return flowContextPutDataOperation.putFlowContextData(key, instance, flowContextDataRange, flowDataFeatures);
    }

    @Override
    public FlowContext putInstanceData(Object instance, Class classType, FlowContextDataRange flowContextDataRange, FlowDataFeature... flowDataFeatures) {
        if(null==instance){
            return flowExecutorConfig.flowContext;
        }
        String key = classType.getName();
        return flowContextPutDataOperation.putFlowContextData(key, instance, flowContextDataRange, flowDataFeatures);
    }
}
